package com.sumansoul.popupwindowdemo;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.sumansoul.popupwindowdemo.popwindow.CommonPopupWindow;

/**
 * 仿IOS弹窗
 */
public class MainActivity extends AppCompatActivity {
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private LinearLayout content;


    /**
     * 弹窗左侧
     */
    private CommonPopupWindow popupWindowLeft;
    private CommonPopupWindow popupWindowRight;
    private CommonPopupWindow popupWindowBottom;
    private CommonPopupWindow popupWindowTop;
    private CommonPopupWindow popupWindow;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }


    private void initView() {
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        btn5 = findViewById(R.id.btn5);
        content = findViewById(R.id.content);

        /**
         * 左侧
         */
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAllLeft(btn1);
            }
        });

        /**
         * 右边边弹出框
         */
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAllRight(btn2);
            }
        });

        /**
         * 中间弹出框
         */
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAllBottom(btn3);
            }
        });

        /**
         * 全屏弹出
         */
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAllWindow();
            }
        });

        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAllWindowTop(btn5);
            }
        });
    }


    /**
     * 左侧弹出
     *
     * @param view
     */
    public void showAllLeft(View view) {

        try {
            if (popupWindowLeft != null && popupWindowLeft.isShowing()) {
                return;
            }
            popupWindowLeft = new CommonPopupWindow.Builder(MainActivity.this)
                    .setView(R.layout.pop_left)
                    //定宽
                    .setWidthAndHeight(view.getMeasuredWidth(), ViewGroup.LayoutParams.WRAP_CONTENT)
                    //取值范围0.0f-1.0f 值越小越暗
                    .setBackGroundLevel(0.5f)
                    .setAnimationStyle(R.style.AnimLeft)
                    //true 点击外侧消失,false 点击外侧不消失
                    .setOutsideTouchable(true)
                    .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                        @Override
                        public void getChildView(View view, int layoutResId) {

                            //1
                            view.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "Android", Toast.LENGTH_SHORT).show();
                                }
                            });
                            //2
                            view.findViewById(R.id.tv2).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "JAVA", Toast.LENGTH_SHORT).show();
                                }
                            });
                            //取消
                            view.findViewById(R.id.pop_cancle).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                                    popupWindowLeft.dismiss();
                                }
                            });
                        }
                    })
                    .create();

            popupWindowLeft.showAsDropDown(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 右侧弹出
     *
     * @param view
     */
    public void showAllRight(View view) {

        try {
            if (popupWindowRight != null && popupWindowRight.isShowing()) {
                return;
            }
            popupWindowRight = new CommonPopupWindow.Builder(MainActivity.this)
                    .setView(R.layout.pop_left)
                    //定宽
                    .setWidthAndHeight(view.getMeasuredWidth(), ViewGroup.LayoutParams.WRAP_CONTENT)
                    .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                    .setAnimationStyle(R.style.AnimRight)
                    //true 点击外侧消失,false 点击外侧不消失
                    .setOutsideTouchable(true)
                    .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                        @Override
                        public void getChildView(View view, int layoutResId) {

                            //1
                            view.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "Android", Toast.LENGTH_SHORT).show();
                                }
                            });
                            //2
                            view.findViewById(R.id.tv2).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "JAVA", Toast.LENGTH_SHORT).show();
                                }
                            });
                            //取消
                            view.findViewById(R.id.pop_cancle).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                                    popupWindowRight.dismiss();
                                }
                            });
                        }
                    })
                    .create();
            //显示
            popupWindowRight.showAsDropDown(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 中间弹出框
     *
     * @param view
     */
    public void showAllBottom(View view) {

        try {
            if (popupWindowBottom != null && popupWindowBottom.isShowing()) {
                return;
            }
            popupWindowBottom = new CommonPopupWindow.Builder(MainActivity.this)
                    .setView(R.layout.pop_left)
                    //定宽
                    .setWidthAndHeight(view.getMeasuredWidth(), ViewGroup.LayoutParams.WRAP_CONTENT)
                    .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                    .setAnimationStyle(R.style.AnimMiddle)
                    //true 点击外侧消失,false 点击外侧不消失
                    .setOutsideTouchable(true)
                    .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                        @Override
                        public void getChildView(View view, int layoutResId) {

                            //1
                            view.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "Android", Toast.LENGTH_SHORT).show();
                                }
                            });
                            //2
                            view.findViewById(R.id.tv2).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "JAVA", Toast.LENGTH_SHORT).show();
                                }
                            });
                            //取消
                            view.findViewById(R.id.pop_cancle).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                                    popupWindowBottom.dismiss();
                                }
                            });
                        }
                    })
                    .create();
            //显示
            popupWindowBottom.showAsDropDown(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 全屏弹出
     */
    public void showAllWindow() {

        try {
            if (popupWindow != null && popupWindow.isShowing()) {
                return;
            }

            popupWindow = new CommonPopupWindow.Builder(MainActivity.this)
                    .setView(R.layout.pop_select_model_icon)
                    //全屏
                    .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                    .setAnimationStyle(R.style.AnimDown)
                    //true 点击外侧消失,false 点击外侧不消失
                    .setOutsideTouchable(true)
                    .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                        @Override
                        public void getChildView(View view, int layoutResId) {

                            //1
                            view.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "超清", Toast.LENGTH_SHORT).show();
                                }
                            });
                            //2
                            view.findViewById(R.id.tv2).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "高清", Toast.LENGTH_SHORT).show();
                                }
                            });
                            //取消
                            view.findViewById(R.id.pop_cancle).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                                    popupWindow.dismiss();
                                }
                            });
                        }
                    })
                    .create();

            //view 随便一个父类窗体
            popupWindow.showAtLocation(content, Gravity.BOTTOM, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    /**
     * 上方弹出框
     *
     * @param view
     */
    public void showAllWindowTop(View view) {

        try {
            if (popupWindowTop != null && popupWindowTop.isShowing()) {
                return;
            }
            popupWindowTop = new CommonPopupWindow.Builder(MainActivity.this)
                    .setView(R.layout.pop_left)
                    //定宽
                    .setWidthAndHeight(view.getMeasuredWidth(), ViewGroup.LayoutParams.WRAP_CONTENT)
                    .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                    .setAnimationStyle(R.style.AnimTop)
                    //true 点击外侧消失,false 点击外侧不消失
                    .setOutsideTouchable(true)
                    .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                        @Override
                        public void getChildView(View view, int layoutResId) {

                            //1
                            view.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "Android", Toast.LENGTH_SHORT).show();
                                }
                            });
                            //2
                            view.findViewById(R.id.tv2).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "JAVA", Toast.LENGTH_SHORT).show();
                                }
                            });
                            //取消
                            view.findViewById(R.id.pop_cancle).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                                    popupWindowTop.dismiss();
                                }
                            });
                        }
                    })
                    .create();
             //弹窗高度和view本身高度
            int allHeight = popupWindowTop.getHeight()+view.getMeasuredHeight();
            Log.d("LUO","高度===="+allHeight);
            //显示
            popupWindowTop.showAsDropDown(view,0,-allHeight);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

